﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DatVeB2 : System.Web.UI.Page
{
    Controllers controller;
    List<ChuyenBay> listChuyenBayDi;
    List<ChuyenBay> listChuyenBayVe;

    protected void Page_Load(object sender, EventArgs e)
    {
        controller = new Controllers();
        if (
            Session["LoaiChuyenBay"] != null
            && Session["IDsanBayDi"] != null
            && Session["IDsanBayDen"] != null
            && Session["TensanBayDi"] != null
            && Session["TensanBayDen"] != null
            && Session["NgayDi"] != null
            && Session["NgayDen"] != null
            && Session["Adults"] != null)
        {
            if (!IsPostBack)
            {
                LoadChuyenBay();
                Label2.Visible = false;
                if (Session["LoaiChuyenBay"].ToString().Equals("khu hoi"))
                {
                    Label10.Visible = false;
                    Label3.Text = "Khứ hồi";
                }
                else
                {
                    Label10.Visible = true;
                    Label3.Text = "Một chiều";
                }
                //DateTime ngayDi = DateTime.Parse(Session["NgayDi"].ToString());
                //Label3.Text = ngayDi.Day.ToString();
                //Label4.Text = ngayDi.Month.ToString();
                //Label5.Text = ngayDi.Year.ToString();
            }
        }
        else
        {
            Response.Redirect("Home.aspx");
        }
        
    }

    private void LoadChuyenBay()
    {
        string maSBdi = Session["IDsanBayDi"].ToString();
        string maSBden = Session["IDsanBayDen"].ToString();
        string tenSBden = Session["TensanBayDen"].ToString();
        string tenSBdi = Session["TensanBayDi"].ToString();
        DateTime ngayDi = DateTime.Parse(Session["NgayDi"].ToString());
        

        string loaiChuyenBay = "Mot Chieu";

        listChuyenBayDi = controller.TimKiemChuyenBay(maSBdi, maSBden, ngayDi, ngayDi, loaiChuyenBay);

        List<ChuyenBayChon> listCBDishowed = new List<ChuyenBayChon>();

        foreach (ChuyenBay cb in listChuyenBayDi)
        {
            for (int i = 0; i < cb.CacHangGhe.Count; i++)
            {
                ChuyenBayChon cbc = new ChuyenBayChon();

                cbc.MaCB = cb.MaChuyenBay;
                cbc.TenCB = tenSBdi + " - " + tenSBden;

                GioBay gioBay = new GioBay();
                gioBay = controller.TimGioBay(cb.MaChuyenBay);

                DateTime ngayGioDi = new DateTime();
                ngayGioDi = SetDateTime(cb.ThoiGianDi, gioBay.GioDi);
                cbc.NgayGioDi = ngayGioDi;

                DateTime ngayGioDen = new DateTime();
                ngayGioDen = SetDateTime(cb.ThoiGianDen, gioBay.GioDen);
                cbc.NgayGioDen = ngayGioDen;

                cbc.LoaiVe = cb.CacHangGhe[i];
                cbc.GiaVe = cb.Gia[i];
                cbc.DonViTien = cb.DonViTien;

                listCBDishowed.Add(cbc);
            }
        }

        if (listCBDishowed.Count == 0)
        {
            Label1.Visible = true;
        }
        else
        {
            Label1.Visible = false;
            GridView1.DataSource = listCBDishowed;
            GridView1.DataBind();
        }

        if (Session["LoaiChuyenBay"].ToString().Equals("khu hoi"))
        {
            DateTime ngayDen = DateTime.Parse(Session["NgayDen"].ToString());
            listChuyenBayDi.Clear();
            listChuyenBayDi = controller.TimKiemChuyenBay(maSBden, maSBdi, ngayDi, ngayDen, loaiChuyenBay);

            List<ChuyenBayChon> listCBVeshowed = new List<ChuyenBayChon>();

            foreach (ChuyenBay cb in listChuyenBayDi)
            {
                for (int i = 0; i < cb.CacHangGhe.Count; i++)
                {
                    ChuyenBayChon cbc = new ChuyenBayChon();

                    cbc.MaCB = cb.MaChuyenBay;
                    cbc.TenCB = tenSBdi + " - " + tenSBden;

                    GioBay gioBay = new GioBay();
                    gioBay = controller.TimGioBay(cb.MaChuyenBay);

                    DateTime ngayGioDi = new DateTime();
                    ngayGioDi = SetDateTime(cb.ThoiGianDi, gioBay.GioDi);
                    cbc.NgayGioDi = ngayGioDi;

                    DateTime ngayGioDen = new DateTime();
                    ngayGioDen = SetDateTime(cb.ThoiGianDen, gioBay.GioDen);
                    cbc.NgayGioDen = ngayGioDen;

                    cbc.LoaiVe = cb.CacHangGhe[i];
                    cbc.GiaVe = cb.Gia[i];
                    cbc.DonViTien = cb.DonViTien;

                    listCBVeshowed.Add(cbc);
                }
            }

            if (listCBVeshowed.Count == 0)
            {
                Label9.Visible = true;
            }
            else
            {
                Label9.Visible = false;
                GridView2.DataSource = listCBVeshowed;
                GridView2.DataBind();
            }
        }

    }


    private DateTime SetDateTime(DateTime day, string time)
    {
        DateTime ngayGioDi = new DateTime(day.Year, day.Month, day.Day, int.Parse(time.Substring(0, 2)), int.Parse(time.Substring(3, 2)), 00);
        return ngayGioDi;
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Session.Remove("LoaiChuyenBay");
        Session.Remove("IDsanBayDi");
        Session.Remove("IDsanBayDen");
        Session.Remove("TensanBayDi");
        Session.Remove("TensanBayDen");
        Session.Remove("NgayDi");
        Session.Remove("NgayDen");
        Session.Remove("Adults");
        Session.Remove("Children2-12");
        Session.Remove("Childrenless2");

        Response.Redirect("Home.aspx#divDatVe");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Label1.Visible == true)
        {
            return;
        }
        
        if (GridView1.SelectedIndex < 0) //chưa chọn chuyến bay đi
        {
            Label2.Visible = true;
            return;
        }

        if (Session["LoaiChuyenBay"].ToString().Equals("khu hoi") )
        {
            if (GridView2.SelectedIndex < 0) //chưa chọn chuyến bay về
            {
                Label10.Visible = true;
                return;
            }
        }

        //cả hai đều chọn hết rồi

        //chuyến bay đi
        Session.Remove("MaCBDi");
        Session.Remove("HangDi");
        Session.Add("MaCBDi", GridView1.SelectedRow.Cells[1].Text);
        Session.Add("HangDi", GridView1.SelectedRow.Cells[5].Text);
        
        ChuyenBayChon cbc1 = new ChuyenBayChon();
        cbc1.MaCB = GridView1.SelectedRow.Cells[1].Text;
        cbc1.TenCB = GridView1.SelectedRow.Cells[2].Text;
        cbc1.NgayGioDi = DateTime.Parse(GridView1.SelectedRow.Cells[3].Text);
        cbc1.NgayGioDen = DateTime.Parse(GridView1.SelectedRow.Cells[4].Text);
        cbc1.LoaiVe = GridView1.SelectedRow.Cells[5].Text;
        cbc1.GiaVe = double.Parse(GridView1.SelectedRow.Cells[6].Text);
        cbc1.DonViTien = GridView1.SelectedRow.Cells[7].Text;

        Session.Add("CBDiDuocChon", cbc1);
        //chuyến bay về
        if (Session["LoaiChuyenBay"].ToString().Equals("khu hoi"))
        {
            Session.Add("MaCBVe", GridView2.SelectedRow.Cells[1].Text);
            Session.Add("HangVe", GridView2.SelectedRow.Cells[5].Text);

            ChuyenBayChon cbc2 = new ChuyenBayChon();
            cbc2.MaCB = GridView2.SelectedRow.Cells[1].Text;
            cbc2.TenCB = GridView2.SelectedRow.Cells[2].Text;
            cbc2.NgayGioDi = DateTime.Parse(GridView2.SelectedRow.Cells[3].Text);
            cbc2.NgayGioDen = DateTime.Parse(GridView2.SelectedRow.Cells[4].Text);
            cbc2.LoaiVe = GridView2.SelectedRow.Cells[5].Text;
            cbc2.GiaVe = double.Parse(GridView2.SelectedRow.Cells[6].Text);
            cbc2.DonViTien = GridView2.SelectedRow.Cells[7].Text;

            Session.Add("CBDVeDuocChon", cbc2);

        }
        Response.Redirect("DatVeB3.aspx#divDatVeB");

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label2.Visible = false;
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label10.Visible = false;
    }

}